package com.hula.onion.book.server.spider.platforms.wangcai51;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.model.AccountInfo;
import com.hula.onion.book.server.utils.MoneyUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Create Date:2018/1/17 19:49
 *
 * @author shan
 * @version 1.0
 */
@Component
public class WangCai51SpiderHelper {

    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form, String publicKey) throws Exception {

        Map<String, Object> map = new HashMap<>(3);

        map.put("username", form.getUserName());
        map.put("password", DigestUtils.md5Hex(form.getPassword()));
        map.put("code", form.getTicket());
        map.put("grant_type", "password");
        map.put("action", "login");
        map.put("client_id", "91wangcai");
        map.put("client_secret", "78fijfdsaDjKAL09fdDCV");

        return map;

    }

    /**
     * 解析账户余额
     *
     * @param json
     * @return
     */
    public AccountInfo parseAccountBalance(String json) {
        AccountInfo accountInfo = new AccountInfo();
        //{"status":200,"message":"请求成功","data":{"wc_total":"0.00","total":"0.0","wc_balance":"0.00","qb_total":"0.00","frost":"0.0","isBilateralUser":0,"award_status":"2","balance":"0.00","qb_balance":"0.00","old_await":"0.00","getMoney":"0.00","old_balance":"0.00","income":"0.00","wc_frost":"0.00","user_id":1214847,"old_total":"0.00","isCheckHistory":"1","wc_await":"0.00","priority_await":"0","qb_await":"0.00","old_frost":"0.00","await":"0.0","end_time":"2016.03.29 00:00:00","qb_frost":"0.00","gains":"0.00"},"page":null}
        JSONObject funInfo = JSON.parseObject(json)
                .getJSONObject("data");
        Long accountInteger = funInfo.getLong("balance");
        accountInfo.setAmount(new BigDecimal(MoneyUtils.liToYuan(accountInteger)));

        return accountInfo;
    }


}
